﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using IndieStudio.BugsBang.Utility;

///Developed By Indie Studio
///https://assetstore.unity.com/publishers/9268
///www.indiestd.com
///info@indiestd.com

namespace IndieStudio.BugsBang.Game
{
	[DisallowMultipleComponent]
	public class WinDialog : MonoBehaviour
	{
		/// <summary>
		/// Win dialog animator.
		/// </summary>
		public Animator WinDialogAnimator;

		/// <summary>
		/// The level title text.
		/// </summary>
		public Text levelTitle;
			
		/// <summary>
		/// The level score text.
		/// </summary>
		public Text levelScore;

		/// <summary>
		/// The stars slider/image fill.
		/// </summary>
		public ImageFill starsSlide;

		/// <summary>
		/// The visible flag.
		/// </summary>
		[HideInInspector]
		public bool visible;

		[HideInInspector]
		/// <summary>
		/// The stars number of the dialog.
		/// </summary>
		public LevelsManager.Level.StarsNumber starsNumber;

		/// <summary>
		/// The stars of the dialog.
		/// </summary>
		public Transform[] stars;

		/// <summary>
		/// A static instance of this class.
		/// </summary>
		public static WinDialog instance;

		void Awake ()
		{
			if (instance == null) {
				instance = this;
			}
		}

		// Use this for initialization
		void Start ()
		{
			//Setting up the references
			visible = false;

			if (WinDialogAnimator == null) {
				WinDialogAnimator = GetComponent<Animator> ();
			}

			SetActiveFalse ();
		}

		/// <summary>
		/// Show the Win Dialog.
		/// </summary>
		public void Show ()
		{
			if (WinDialogAnimator == null) {
				return;
			}

			SetActiveTrue ();

			//Play level completed sound effect
			AudioSources.instance.PlayCompletedSFX ();

			BlackArea.instance.Show ();
			WinDialogAnimator.SetTrigger ("Running");
			AdsManager.instance.ShowAdvertisment (AdPackage.AdEvent.Event.ON_SHOW_WIN_DIALOG,null);
			visible = true;

			//Fill stars slider
			int starsCount = CommonUtil.LevelStarsNumberEnumToIntNumber (starsNumber);
			StartCoroutine (RunStarsFillImage (starsCount / 3.0f));
		}

		/// <summary>
		/// Hide the Win Dialog.
		/// </summary>
		public void Hide ()
		{
			BlackArea.instance.Hide ();
			StopAllCoroutines ();
			WinDialogAnimator.SetBool ("Running", false);
			visible = false;
			AdsManager.instance.HideAdvertisment ();
			GameObject.FindObjectOfType<SceneStartup> ().ShowAd ();
			SetActiveFalse ();
		}

		/// <summary>
		/// Set gameobject active false.
		/// </summary>
		public void SetActiveFalse(){
			gameObject.SetActive (false);
		}

		/// <summary>
		/// Set gameobject active true.
		/// </summary>
		public void SetActiveTrue(){
			gameObject.SetActive (true);
		}

		/// <summary>
		/// Set the level title.
		/// </summary>
		/// <param name="value">Value.</param>
		public void SetLevelTitle (string value)
		{
			if (string.IsNullOrEmpty (value) || levelTitle == null) {
				return;
			}
			levelTitle.text = value;
		}

		/// <summary>
		/// Set the score.
		/// </summary>
		/// <param name="value">Value.</param>
		public void SetScore (float value)
		{
			if (levelScore == null) {
				return;
			}

			levelScore.text = value.ToString();
		}

		/// <summary>
		/// Set the stars of the dialog.
		/// </summary>
		/// <param name="starsNumber">Stars number.</param>
		public void SetStars(LevelsManager.Level.StarsNumber starsNumber){
			this.starsNumber = starsNumber;
			CommonUtil.SetStarsRating (starsNumber, stars [0], stars [1], stars [2]);
		}

		/// <summary>
		/// Run the stars slider/fill image.
		/// </summary>
		/// <param name="value">Value of fill amount.</param>
		private IEnumerator RunStarsFillImage(float value){
			yield return new WaitForSeconds (0.2f);
			if (starsSlide != null) {
				starsSlide.fromValue = 0;
				starsSlide.toValue = value;
				starsSlide.Run ();
			}
		}
	}
}